﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SamuelBlanchard.Extenders
{
    public static class AnimationExtender
    {
        /// <summary>
        /// FadeIn
        /// </summary>
        /// <param name="element"></param>
        /// <param name="IsFadeIn"></param>

        public static Storyboard AnimateDouble(this UIElement element, double from, double to, string propertyName, double durationInMilliseconds = 300, Action<UIElement> completed = null, double? beginTimeInMilliseconds = null)
        {
            Storyboard storyboard = new Storyboard();
            storyboard.Duration = new Duration(TimeSpan.FromMilliseconds(durationInMilliseconds));

            DoubleAnimation animation = new DoubleAnimation() { From = from, To = to };
            animation.Duration = storyboard.Duration;
            Storyboard.SetTarget(animation, element);
            Storyboard.SetTargetProperty(animation, new PropertyPath(propertyName));

            storyboard.Children.Add(animation);

            if (beginTimeInMilliseconds.HasValue == true)
            {
                storyboard.BeginTime = TimeSpan.FromMilliseconds(beginTimeInMilliseconds.Value);
            }

            EventHandler handlerCompleted = null;
            handlerCompleted = (object sender, EventArgs e) =>
            {
                storyboard.Completed -= handlerCompleted;

                if (completed != null)
                {
                    completed(element);
                }
            };

            storyboard.Completed += handlerCompleted;

            storyboard.Begin();

            return storyboard;
        }

        /// <summary>
        /// FadeIn
        /// </summary>
        /// <param name="element"></param>
        /// <param name="durationInMillisecondes"></param>
        /// <param name="beginTimeInMilliseconds"></param>

        public static Storyboard FadeIn(this UIElement element, Action<UIElement> completed = null, double durationInMilliseconds = 500, double? beginTimeInMilliseconds = null)
        {
            return AnimateDouble(element, 0.0, 1.0, "Opacity", durationInMilliseconds, completed, beginTimeInMilliseconds);
        }

        /// <summary>
        /// FadeOut
        /// </summary>
        /// <param name="element"></param>
        /// <param name="durationInMillisecondes"></param>
        /// <param name="beginTimeInMilliseconds"></param>

        public static Storyboard FadeOut(this UIElement element, Action<UIElement> completed = null, double durationInMilliseconds = 500, double? beginTimeInMilliseconds = null)
        {
            return AnimateDouble(element, 1.0, 0.0, "Opacity", durationInMilliseconds, completed, beginTimeInMilliseconds);
        }
    }
}

